package gameSite;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class EditItem
 */
@WebServlet("/EditItem")
public class EditItem extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Connection conn;
	Statement stmt = null;
	ResultSet rs = null; 
	Product inventory;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditItem() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		getItemDetails(request);
		request.getRequestDispatcher("editItem.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	protected void getItemDetails(HttpServletRequest request){
		String id = request.getParameter("id");
		String sql;
		inventory = new Product();
		try{
	      System.out.println("Creating statement...");
	      stmt = RemoteConnection.getConnection().createStatement();
	      sql = "select Products.productID as ID, gameName, details, console, typeOfGame,price, image, numberOfProducts from Products join Inventory on Inventory.productID = Products.productID where Products.productID="+id;
	      rs = stmt.executeQuery(sql);
	      while (rs.next()){
	    	  inventory.setProductID(Integer.parseInt(rs.getString("ID")));
	    	  inventory.setGameName(rs.getString("gameName"));
	    	  inventory.setConsole(rs.getString("console"));
	    	  inventory.setTypeOFGame(rs.getString("typeofGame"));
	    	  inventory.setDetails(rs.getString("details"));
	    	  inventory.setPrice(Double.parseDouble(rs.getString("price")));
	    	  inventory.setImage(rs.getString("image"));
	    	  inventory.setNumberOfProducts(Integer.parseInt(rs.getString("numberOfProducts")));
	      }
	    	  rs.close();
		      stmt.close();
		      RemoteConnection.closeConnection();
		      request.setAttribute("itemID", inventory.getProductID());
		      request.setAttribute("itemName", inventory.getGameName());
		      request.setAttribute("category", inventory.getTypeOfGame());
		      request.setAttribute("console", inventory.getConsole());
		      request.setAttribute("price", inventory.getPrice());
		      request.setAttribute("details", inventory.getDetails());
		      request.setAttribute("availability", inventory.getNumberOfProducts());
		      request.setAttribute("image", inventory.getImage());
		      request.setAttribute("type", "2");
		}
		catch(SQLException se){
		      //Handle errors for JDBC
		      se.printStackTrace();
		   }
		
	}

}
